package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.PopUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: AccountSafelyActivity
 * @Description: 账户安全
 * @Author: ljp
 * @CreateDate: 2018/10/10 19:05
 * @Version: 1.0
 */
public class AccountSafelyActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_phone_num)
    TextView tvPhoneNum;
    @BindView(R.id.linear_account_safely)
    LinearLayout linearAccountSafely;
    @BindView(R.id.tv_password)
    TextView tvPassword;
    @BindView(R.id.linear_password)
    LinearLayout linearPassword;
    @BindView(R.id.linear_pay_way)
    LinearLayout linearPayWay;
    @BindView(R.id.image_way)
    ImageView imageWay;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String TAG = "AccountSafelyActivity";
    private String accountId;
    private String name;
    private boolean isRela = true;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_account_safely;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        initView();
        initHttp();
    }
    /**
     * @return
     * @method
     * @description 查询支付宝账号
     * @date: 2018/11/19 9:40
     * @author: ljp
     */
    private void initHttp() {
        MeInfoService.queryAccount(mySeleInfo.getId(), SPUtils.getEditoString(SPUtils.SP_TOKEN), PhoneUtils.getDeviceInfo(context)).subscribe(mySelfInfoBean -> {
           Log.i(TAG, "initHttp: ---getRealNamestatus--"+mySelfInfoBean.getCode());
           // Log.i(TAG, "initHttp: ---getObject--"+mySelfInfoBean.getObject().toString());
            if (mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_REALNAME_){
                linearPayWay.setEnabled(false);
               // isRela = false;
                toast_MakeText(mySelfInfoBean.getMsg());
            }else if (mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_REALNAME){
                isRela = false;
                toast_MakeText(mySelfInfoBean.getMsg());
            }else if (mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                accountId = mySelfInfoBean.getObject().getAccountId();
                name = mySelfInfoBean.getObject().getName();
                if(mySelfInfoBean.getObject().getRealNamestatus() == 1) {
                    if (!TextUtils.isEmpty(mySelfInfoBean.getObject().getAccount())) {
                        initData(mySelfInfoBean.getObject());
                    }
                }else {
                    isRela = false;
                }
            }else if(mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(mySelfInfoBean.getMsg());
            }
        }, throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
            Log.i(TAG, "initHttp: -----" + throwable.toString());
        });
    }

    private void initData(MySelfInfoBean.ObjectBean mySeleInfo) {
        imageWay.setVisibility(View.VISIBLE);
        tvAccount.setText(Utils.getPhoneNumber(mySeleInfo.getAccount()));
        linearPayWay.setEnabled(false);
    }
    /**
     * @return
     * @method
     * @description 布局控件数据实例化
     * @date: 2018/10/10 19:20
     * @author: ljp
     */
    private void initView() {
        imageWay.setImageResource(R.mipmap.zhifubao_icon);
        tvPhoneNum.setText(Utils.getPhoneNumber(SPUtils.getEditoString(SPUtils.SP_PHONE)));

    }

    @OnClick({R.id.linear_back, R.id.linear_password, R.id.linear_pay_way})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.linear_password:
                Intent intent = new Intent(activity, BindingPhoneActivity.class);
                startActivity(intent);
                break;
            case R.id.linear_pay_way:
                //showPopuWay(view);
                if(!isRela){
                    Intent intent1 = new Intent(activity, RealNameActivity.class);
                    startActivity(intent1);
                    finish();
                }else {
                    Intent intent1 = new Intent(activity, AlipayBindingActivity.class);
                    intent1.putExtra("accountId",accountId);
                    intent1.putExtra("name",name);
                    startActivity(intent1);
                    finish();
                }
                break;
        }
    }

    /**
     * @return
     * @method
     * @description 选择支付方式popu
     * @date: 2018/10/9 16:36
     * @author: ljp
     */
    private void showPopuWay(View v) {
        View view = View.inflate(context, R.layout.popu_account_way, null);
        PopUtils.showPopupWindow(this, view, false);
        LinearLayout back = view.findViewById(R.id.linear_close);
        LinearLayout linear_z = view.findViewById(R.id.linear_zhifubao);
        LinearLayout linear_w = view.findViewById(R.id.linear_weixin);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtils.hidePopupWindow();
            }
        });
        linear_w.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtils.hidePopupWindow();
                imageWay.setVisibility(View.VISIBLE);
                imageWay.setImageResource(R.mipmap.weixin_icon);
            }
        });
        linear_z.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtils.hidePopupWindow();
                imageWay.setVisibility(View.VISIBLE);
                imageWay.setImageResource(R.mipmap.zhifubao_icon);
            }
        });
    }
}
